package planit.view;
import java.awt.event.ActionEvent;
import java.util.ArrayList;

import javax.swing.AbstractAction;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.ListModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import planit.controller.ControllerFactory;
import planit.controller.ControllerFactory.ControllerType;
import planit.model.Model;
import planit.model.Person;
import planit.view.wrappers.ModelCellRenderer;
import planit.view.wrappers.PersonWrapper;


/**
 * This code was edited or generated using CloudGarden's Jigloo
 * SWT/Swing GUI Builder, which is free for non-commercial
 * use. If Jigloo is being used commercially (ie, by a corporation,
 * company or business for any purpose whatever) then you
 * should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details.
 * Use of Jigloo implies acceptance of these licensing terms.
 * A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
 * THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
 * LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
@SuppressWarnings("serial")
public class DeletePersonDialog extends javax.swing.JDialog {

	{
		//Set Look & Feel
		try {
			javax.swing.UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
		} catch(Exception e) {
			e.printStackTrace();
		}
	}

	private JLabel selectLabel;
	private JList personList;
	private JTextPane emailAddressPane;
	private AbstractAction cancelAction;
	private JScrollPane jScrollPane1;
	private JLabel emailAddressLabel;
	private JTextPane telephoneNumberPane;
	private JLabel telephoneNumberLabel;
	private JTextPane namePane;
	private JLabel nameLabel;
	private JButton cancelButton;
	private JButton deleteButton;
	private Model model;

	/**
	 * Auto-generated main method to display this JDialog
	 */

	public DeletePersonDialog(Model model) {
		super();
		this.model = model;
		initGUI();
		updatePersonList();
	}

	private void initGUI() {
		try {
			{
				getContentPane().setLayout(null);
				{
					selectLabel = new JLabel();
					getContentPane().add(selectLabel);
					getContentPane().add(getJScrollPane1());
					selectLabel.setText("Select person");
					selectLabel.setBounds(6, 12, 109, 16);
				}
				{
					deleteButton = new JButton();
					getContentPane().add(deleteButton);
					deleteButton.setAction(ControllerFactory
							.getController(ControllerType.DELETE_PERSON));
					deleteButton.getAction().putValue("DeletePersonDialog", this);
					deleteButton.setText("Delete");
					deleteButton.setBounds(6, 223, 115, 39);
				}
				{
					cancelButton = new JButton();
					getContentPane().add(cancelButton);
					cancelButton.setAction(getCancelAction());
					cancelButton.setText("Cancel");
					cancelButton.setBounds(245, 234, 67, 28);
				}
				{
					nameLabel = new JLabel();
					getContentPane().add(nameLabel);
					nameLabel.setText("Person's name");
					nameLabel.setBounds(121, 34, 84, 16);
				}
				{
					namePane = new JTextPane();
					getContentPane().add(namePane);
					namePane.setBounds(121, 50, 191, 24);
					namePane.setEditable(false);
				}
				{
					telephoneNumberLabel = new JLabel();
					getContentPane().add(telephoneNumberLabel);
					telephoneNumberLabel.setText("Telephone Number");
					telephoneNumberLabel.setBounds(121, 80, 107, 16);
				}
				{
					telephoneNumberPane = new JTextPane();
					getContentPane().add(telephoneNumberPane);
					telephoneNumberPane.setBounds(121, 96, 191, 24);
					telephoneNumberPane.setEditable(false);
				}
				{
					emailAddressLabel = new JLabel();
					getContentPane().add(emailAddressLabel);
					emailAddressLabel.setText("E-mail");
					emailAddressLabel.setBounds(121, 126, 36, 16);
				}
				{
					emailAddressPane = new JTextPane();
					getContentPane().add(emailAddressPane);
					emailAddressPane.setBounds(121, 148, 191, 24);
					emailAddressPane.setEditable(false);
				}
			}
			this.setSize(334, 306);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void updatePersonList(){
		Person[] person = new Person[0];
		ArrayList<JLabel> allPersons = new ArrayList<JLabel>();
		person = model.getSchedule().getPersons().toArray(person);
		for (int i = 0; i < person.length; i++) {
			allPersons.add(new PersonWrapper(person[i]));
		}
		ListModel list2Model = new DefaultComboBoxModel(allPersons.toArray());
		personList.setModel(list2Model);
		personList.setCellRenderer(new ModelCellRenderer());
	}

	private void personListValueChanged(ListSelectionEvent evt) {
		if (personList.getSelectedValue() == null) {
			namePane.setText("");
			telephoneNumberPane.setText("");
			emailAddressPane.setText("");
		} else {
			Person temp;
			temp = model.getSchedule().getPerson(
					((PersonWrapper)personList
							.getSelectedValue())
							.getId());

			namePane.setText(temp.getName());
			telephoneNumberPane.setText(temp.getPhoneNumber());
			emailAddressPane.setText(temp.getEmailAddress());
		}
	}

	private JScrollPane getJScrollPane1() {
		if(jScrollPane1 == null) {
			jScrollPane1 = new JScrollPane();
			jScrollPane1.setBounds(6, 34, 109, 183);
			{
				ListModel personListModel = 
					new DefaultComboBoxModel(
							new String[] { "Item One", "Item Two" });
				personList = new JList();
				jScrollPane1.setViewportView(personList);
				personList.setModel(personListModel);
				personList.setBounds(6, 34, 109, 183);
				personList.setPreferredSize(new java.awt.Dimension(82, 177));
				personList.addListSelectionListener(new ListSelectionListener() {
					public void valueChanged(ListSelectionEvent evt) {
						personListValueChanged(evt);
					}
				});
			}
		}
		return jScrollPane1;
	}
	
	public int getPersonId() {
		return ((PersonWrapper)personList
				.getSelectedValue()).getId();
	}
	
	private AbstractAction getCancelAction() {
		if(cancelAction == null) {
			cancelAction = new AbstractAction("Cancel", null) {
				public void actionPerformed(ActionEvent evt) {
					dispose();
				}
			};
		}
		return cancelAction;
	}
}
